package com.pigdogbay.anagramsolver;

import com.pigdogbay.anagramsolver.presenters.AnagramPresenter;
import com.pigdogbay.anagramsolver.presenters.IAnagramView;
import com.pigdogbay.anagramsolver.presenters.NullAnagramView;
import com.pigdogbay.androidutils.usercontrols.GoProNagBox;
import com.pigdogbay.androidutils.utils.ActivityUtils;

import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.EditorInfo;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.TextView.OnEditorActionListener;

public class SearchFragment extends ListFragment implements IAnagramView, GoProNagBox.IGoProResult{
	
	private static final String EVENT_CATEGORY = "Search";
	private ArrayAdapter<String> _ItemAdapter;
	EditText _EditQuery;
	Button _BtnSearch;
	TextView _TextStatus;
	AnagramPresenter _AnagramPresenter;
	private GoProNagBox _GoProNagBox;
	
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_search, container, false);
        _BtnSearch = (Button)rootView.findViewById(R.id.searchBtnSearch);
        _BtnSearch.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				onSearchClick();
			}
		});
        _EditQuery = (EditText) rootView.findViewById(R.id.searchEditQuery);
        _EditQuery.setOnEditorActionListener(new OnEditorActionListener() {
			@Override
			public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
				return onEditDone(actionId);
			}
		});
        _TextStatus = (TextView) rootView.findViewById(R.id.searchTextStatus);
       
		_AnagramPresenter = MainActivity.getAnagramPresenter();
		_ItemAdapter = new ArrayAdapter<String>(this.getActivity(),
				android.R.layout.simple_list_item_1, _AnagramPresenter.getMatchesArray());
		setListAdapter(_ItemAdapter);
        return rootView;
    }
    
    /* 
     * Add ability to look up words on a long press
     */
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
    	super.onActivityCreated(savedInstanceState);
		getListView().setOnItemLongClickListener(new OnItemLongClickListener()
		{
			public boolean onItemLongClick(AdapterView<?> arg0, View v,
					int position, long id)
			{
				try
				{
					String url = _AnagramPresenter.getWordURL(position);
					ActivityUtils.ShowWebPage(getActivity(), url);
				} catch (Exception e){}
				return true;
			}
		});
		_GoProNagBox = new GoProNagBox(getActivity());
		_GoProNagBox.setFrequency(50)
		.setResultListener(this)
		.setUrlId(R.string.market_anagramsolverpro);
    }
    @Override
    public void onResume() {
    	super.onResume();
		_AnagramPresenter.setView(this);
    	_AnagramPresenter.resume();
    }
    @Override
    public void onPause() {
    	super.onPause();
    	_AnagramPresenter.setView(NullAnagramView.GetInstance());
    	_AnagramPresenter.pause();
    	_GoProNagBox.save();
    }
    
    private void onSearchClick()
    {
    	_AnagramPresenter.Search();
    	ActivityUtils.hideKeyboard(getActivity(),_EditQuery.getWindowToken());
		_GoProNagBox.check();
    }
    private boolean onEditDone(int actionId)
    {
		if (actionId == EditorInfo.IME_ACTION_DONE) {
			onSearchClick();
		}
		return false;
    }
    
    /*
     * IAnagramView methods
     */
    
	@Override
	public String getQuery() {
		return _EditQuery.getText().toString();
	}
	@Override
	public void setQuery(String query) {
		_EditQuery.setText(query);
	}
	@Override
	public void setError(String msg) {
		_EditQuery.setError(msg);
	}
	@Override
	public void updateList() {
		_ItemAdapter.notifyDataSetChanged();	
	}
	@Override
	public void showKeyboard()
	{
		ActivityUtils.showKeyboard(getActivity(), _EditQuery);
	}
	@Override
	public void setStatus(String status, boolean busy) {
		_TextStatus.setText(status);
	}
	@Override
	public String getStringResource(int id) {
		return getActivity().getString(id);
	}
	@Override
	public void showLoadingDictionary(){
		_BtnSearch.setText(getActivity()
				.getString(R.string.search_button_loading));
		_BtnSearch.setEnabled(false);		
	}
	@Override
	public void showReady(){
		_BtnSearch.setText(getActivity()
				.getString(R.string.search_button_search));
		_BtnSearch.setEnabled(true);
	}
	@Override
	public void showLoadingDictionaryError()
	{
		setError(getString(R.string.search_dictionary_load_error));
	}
	@Override
	public void trackQuery(String queryType) {
		AnagramSolverApplication.trackEvent(this, EVENT_CATEGORY, queryType);
	}

	@Override
	public void noGoPro() {
		AnagramSolverApplication.trackEvent(this, GoProNagBox.TAG, "No");
	}

	@Override
	public void yesGoPro() {
		AnagramSolverApplication.trackEvent(this, GoProNagBox.TAG, "Yes");
	}
	
}
